<?php
namespace Admin\Controller;
use Think\Controller;
class LoadingController extends BaseController {
    public function loading(){
    	 $this->display();
    }
    //上传文件
    public function upload(){

          $upload = new \Think\Upload();// 实例化上传类
          $upload->maxSize   =     73145728 ;// 设置附件上传大小
          $upload->exts      =     array('xls','xlsx');// 设置附件上传类型
          $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
          $upload->savePath  =     'excel/'; // 设置附件上传（子）目录
         // 上传单个文件 
         $info   =   $upload->uploadOne($_FILES['file']);
         
           if(!$info) {// 上传错误提示错误信息
              echo "false";
            }else{//上传成功 获取上传文件信息
              $filename = $info['savepath'].$info['savename'];
              echo $filename;
           }
            
   }
  //读出excel里的数据写入数据库
  public function insertMysql(){
     // $t1 = microtime(true);
      $path = I('post.path');
      $houzhui = explode('.', $path);
      //写入数据库
         require_once './ThinkPHP/Library/Vendor/Phpexcel/PHPExcel.php';
         require_once './ThinkPHP/Library/Vendor/Phpexcel/PHPExcel/IOFactory.php';
         require_once './ThinkPHP/Library/Vendor/Phpexcel/PHPExcel/Reader/Excel5.php';
         require_once './ThinkPHP/Library/Vendor/Phpexcel/PHPExcel/Reader/Excel2007.php';
         if($houzhui[1]=='xls'){
           $objReader = \PHPExcel_IOFactory::createReader('Excel5');
         }elseif($houzhui[1]=='xlsx'){
           $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
         }
         $path = './Uploads/'.$path;
  
         $objPHPExcel = $objReader->load($path); //$filename可以是上传的文件，或者是指定的文件
         $sheet = $objPHPExcel->getSheet(0);
         $highestRow = $sheet->getHighestRow(); // 取得总行数
         $highestColumn = $sheet->getHighestColumn(); // 取得总列数
         $resouiceModel = D('resource');
        //循环读取excel文件,读取一条,插入一条
          for($j=1;$j<=$highestRow;$j++){
              $c=10000;
               if ($j==1||is_int($j/10000)) {
               
                 $sql = "INSERT INTO cms_resource(pnumber) VALUES";
                 if ($j==1) {
                    $w = $c;
                 }else{
                    $w = $j+$c;
                 }
                
                 if($w>=$highestRow){
                    $w=$highestRow+1;
                 }
                 for ($a=$j; $a<$w; $a++){
                 
                 if($j==$a){
                    $sql .= "('".$objPHPExcel->getActiveSheet()->getCell('A'.$a)->getValue()."')";  
                 }else{
                    $sql .= ",('".$objPHPExcel->getActiveSheet()->getCell('A'.$a)->getValue()."')";
                 }  
               } 
               $res =  $resouiceModel->query($sql); 
                  
               }
            }
          // $t2 = microtime(true);
          // $a = round($t2-$t1,3);
           if($res!==FALSE){
              echo json_encode(array('ok' => 1, ));
              unlink($path);
              rmdir('./Uploads/excel/'.date('Y-m-d'));
           }else{
              echo json_encode(array('ok' => 2, ));
              unlink($path);
              rmdir('./Uploads/excel/'.date('Y-m-d'));
           }
    }
}